	var datagrid;
	var rowEditor=undefined;
	var editRow=undefined;
	var editUrl="";
	$(function(){
		datagrid=$("#dg").datagrid({
			loadMsg:'数据加载中,请稍后...',
			url:"../../api/NJ_searchAllTestStandard",//加载的URL
			method:"get",
		    idField:"template_id",
		    rownumbers: true, //是否加行号
		    singleSelect:true,
			pagination:true,//显示分页
			pageSize:10,//分页大小
			pageList:[10,20,30,40],//每页的个数
			beforePageText: '第',//页数文本框前显示的汉字 
	        afterPageText: '页    共 {pages} 页', 
	        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录', 
			//fit:true,//自动补全			
			width:1200,
            height:400,
			fitColumns:true,
			title:"标准管理",
			columns:[[      //每个列具体内容
				{ field:'ck',checkbox:true
			    },		
				{field:'template_id',title:'标准ID',width:60,//sortable:true//,order:'asc'
				//,hidden:true
				//  editor : {
				// 	type : 'validatebox',
				// 	options : {
				// 		required : true
				// 	}
				// }
				}, 
				{field:'template_name',title:'标准名称',width:200,},
				{field:'template_code',title:'标准编码',width:150,},
				{field:'template_use_area',title:'适用范围',width:120,}, 				 
				{field:'template_type',title:'标准类别',width:80,},					
				{field:'part_type_id',title:'所属产品类别ID',width:100,hidden:true}, 
				{field:'part_type_name',title:'所属产品类别',width:100,},
				{field:'fixscope',title:'测试类别',width:100,},
				{field:'relateddocument',title:'关联文档',width:100,hidden:true},
				{field:'download',title:'下载文档',width:80,
					formatter:function(value,row,index){
						var tmpRow = datagrid.datagrid('getData').rows[index];
						var rd = tmpRow.relateddocument;
						var a="";
						console.log('rd:'+rd);
						if(rd !=null ){
							a = '<a id="a" href="/downLoadFileFromFtp?url='+rd+'" >下载</a>';
						}else{
							a = '<a id="a" href="javascript:void(0)" class="easyui-linkbutton" onclick="alert(\'没有文件\');">下载</a>';
						}
						return a
						
					}
					 
				}, 		
				{field:'remarks',title:'说明',width:50,},						
				{field:'validdeadline',title:'有效截止期',width:100,},
				{field:'update_emp_id',title:'最后修改人工号',width:100,hidden:true},				
				{field:'update_date',title:'最后修改时间',width:100,hidden:true},		
				{field:'isrt_emp_id',title:'添加人工号',width:100,hidden:true},				
				{field:'isrt_date',title:'添加时间',width:100,hidden:true},		
				{field:'delete_status',title:'标志',width:100,hidden:true},
				{field:'isrt_emp_name',title:'添加人',width:60,},
				{field:'update_emp_name',title:'最后修改人',width:100,}, 
				{field:'state',title:'状态',width:60,//hidden:true
				}
		        ]], 

			toolbar:[              //工具条
		        {id:"tool_add",text:"增加",iconCls:"icon-ktsyAdd",handler:function(){//回调函数
		   

					 $('#ef').form('clear');
		        	// $('#edit').dialog('open').dialog('center').dialog('setTitle','增加');
		        	$('#edit').dialog('setTitle','增加');
		        	$('#edit').window('open').window('move',{
		        		left:450,
		        		top:100
		        	});
		        	editUrl="../../api/NJ_insertTestStandard";
		  			$("#ef").form("load",{
            				
            				update_emp_id:userno,
            				update_emp_name:realname,			
            				isrt_emp_id:userno,
            				isrt_emp_name:realname   				
            			});
		        

		        }},
		        {id:"tool_del",text:"删除",iconCls:"icon-ktsyDelete",handler:function(){
		        	var rows=datagrid.datagrid('getSelections');
		  			var templateId=rows[0]["template_id"];
		  			// console.log(rows[0]["template_id"]);
		  			// console.log(rows[0]);
		        	if(rows.length<=0)
		        	{
		        		$.messager.alert('警告','您没有选择','error');
		        	}else
		        	{
		        		$.messager.confirm('确定','您确定要删除吗',function(t){
		        			if(t)
		        			{
		        				$.ajax({
		        					url : '../../api/NJ_deleteTestStandard',
		        					data : rows[0],
		        					dataType : 'json',
		        					success : function(r) {
		        						// console.log(r);
		        						if (r==1) {
		        							datagrid.datagrid('acceptChanges');
		        							$.messager.show({
		        								msg : r.msg,
		        								title : '成功'
		        							});
		        							editRow = undefined;
		        							datagrid.datagrid('reload');
		        							//操作日志
		        							saveSystemLog("标准管理","/menu_nj/TestStandardManage/TestStandardManage","4","删除标准",realname,userno);
		        						} else {
		        							/*datagrid.datagrid('rejectChanges');*/
		        							datagrid.datagrid('beginEdit', editRow);
		        							$.messager.alert('失败', '删除失败!存在关联测试项', 'error',function(){
		        								$.messager.confirm('提示','是否查看关联测试项',function(r){
													if (r){
													    console.log("标准ID："+templateId);
													    
													    window.location.href="../../menu_nj/TestStandardManage/TestItemManage?id="+templateId;
													}
												});
		        							});
		        						}
		        						datagrid.datagrid('unselectAll');
		        					}
		        				});
		        			
		        			}
		        		})
		        	}
		        	
		        	
		        }},
		        {id:"tool_edit",text:"修改",iconCls:"icon-ktsyEdit",handler:function(){
		        	var rows=datagrid.datagrid('getSelections');
					editUrl="../../api/NJ_editTestStandard";
		 
		    		if(rows.length<=0)
		        	{
		        		$.messager.alert('警告','您没有选择','error');
		        	}
		        	else if(rows.length>1)
		        	{
		        		$.messager.alert('警告','不支持批量修改','error');
		        	}
		        	else
		        	{
		        		// $('#edit').dialog('open').dialog('center').dialog('setTitle','修改');
		        		$('#edit').dialog('setTitle','修改');
			        	$('#edit').window('open').window('move',{
			        		left:450,
			        		top:100
			        	});
            			$("#ef").form("clear");
            			var tempState;
            			if(rows[0]["state"]=="有效"){
            				tempState=1;
            			}else{
            				tempState=0;
            			}		             			
            			//console.log(tempState);
            			$("#ef").form("load",{
            				template_id:rows[0]["template_id"],
            				template_code:rows[0]["template_code"],
            				template_name:rows[0]["template_name"],
            				template_use_area:rows[0]["template_use_area"],
            				template_type:rows[0]["template_type"],
            				update_emp_id:userno,
            				update_emp_name:realname,       				
            				isrt_emp_id:rows[0]["isrt_emp_id"],
            				isrt_emp_name:rows[0]["isrt_emp_name"],
            				part_type_id:rows[0]["part_type_id"],
            				part_type_name:rows[0]["part_type_name"],
            				fixscope:rows[0]["fixscope"],
            				relateddocument:rows[0]["relateddocument"],
            				remarks:rows[0]["remarks"],
            				validdeadline:rows[0]["validdeadline"],     
            				state:tempState       				
            			});
            			// if(rows[0]["state"]==0){
            			// 	$("#ef").form("load",{state:"无效"});
            			// }
            			// if (rows[0]["state"]==1) {
            			// 	$("#ef").form("load",{state:"有效"});
            			// };
		        	}
		        }},
		        
		        

		        {id:"tool_stdItem",text:"查看标准项",iconCls:"icon-ktsySearch",handler:function(){
		        	var rows=datagrid.datagrid('getSelections');
		  
		        	if(rows.length<=0)
		        	{
		        		$.messager.alert('警告','您没有选择','error');
		        	}
		        	else if(rows.length>1)
		        	{
		        		$.messager.alert('警告','不支持批量查看','error');
		        	}
		        	else
		        	{
		        		var index=datagrid.datagrid('getRowIndex',datagrid.datagrid('getSelected'))
              			var row1 = datagrid.datagrid('getData').rows[index]; 
              			//alert(row1.id);
              			//alert(row1.template_id);
              			// window.parent.parent.location.href="../../menu/TestStandardManage/TestItemManage?id="+row1.template_id;
              			window.location.href="/menu_nj/TestStandardManage/TestItemManage?id="+row1.template_id;
		        	}
		        }},
		        {id:"tool_import",text:"批量导入",iconCls:"icon-ktsyCopy",handler:function(){
		        	$('#importExcel').window('open').window('move',{
		        		left:450,
		        		top:100
		        	});
		        }}
			]
		});
	});
	


	$(document).ready(function() {
		
		$("#saveButton").click(function saveInfo() {
			var param = $("#ef").serializeArray();		

			$.ajax({
				url: editUrl,
				type: 'get',
				dataType: 'json',
				data: param,
				success:function(r){
					if (r==1) {
						datagrid.datagrid('acceptChanges');
						$.messager.show({
							msg : r.msg,
							title : '成功'
						});
						var operation = "";
						var detail = "";
						if(editUrl == "../../api/NJ_insertTestStandard"){
							operation = "3";
							detail = "添加标准";
						}
						if(editUrl == "../../api/NJ_editTestStandard"){
							operation = "6";
							detail = "修改标准";
						}
						saveSystemLog("标准管理","/menu_nj/TestStandardManage/TestStandardManage",operation,detail,realname,userno);
					}
					if(r==7){
						alert('标准名称不能重复');
						// $.messager.alert('警告','标准名称不能重复','error');
					}
					console.log('r:'+r);
				},
				error:function() {
					alert("错误"+param);
				},
				complete:function(){
					$('#edit').dialog('close');
					datagrid.datagrid('reload');
				}			
			});			
		});
	});
	
	function myformatter(date){
			var y = date.getFullYear();
			var m = date.getMonth()+1;
			var d = date.getDate();
			return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
		}
	function myparser(s){
			if (!s) return new Date();
			var ss = (s.split('-'));
			var y = parseInt(ss[0],10);
			var m = parseInt(ss[1],10);
			var d = parseInt(ss[2],10);
			if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
				return new Date(y,m-1,d);
			} else {
				return new Date();
			}
		}
		function searchByConditions(){
		var std_type = $('#std_type').combobox('getText');
		var pd_type = $('#pd_type').combobox('getValue');
		var std_name = $('#std_name').val();
		var std_code = $('#std_code').val();
		if(pd_type !='' && pd_type !=null){
			
			$('#dg').datagrid('reload',"../../api/NJ_searchTestStandardByCondition?std_type="+std_type+"&pd_type="+pd_type+"&std_name="+std_name+"&std_code="+std_code);
		}
		else{
			// std_code = 0;
			pd_type = 0;
			$('#dg').datagrid('reload',"../../api/NJ_searchTestStandardByCondition?std_type="+std_type+"&pd_type="+pd_type+"&std_name="+std_name+"&std_code="+std_code);
		}

		console.log(std_type);
		console.log(pd_type);
		console.log(std_name);
		console.log(std_code);
		// $('#dg').datagrid('reload',"../../api/searchTestStandardByCondition?std_type="+std_type+"&pd_type="+pd_type+"&std_name="+std_name+"&std_code="+std_code);
	}
	function fileupload(){
	    var maxsize = 8*1024*1024;//8M
	    var obj_file = document.getElementById("houseMaps");
	    var filesize = obj_file.files[0].size;
	    if(filesize>maxsize){
	      alert("文件大小为超过8M,请使用ftp进行上传");
	      return;
	    }
	    var file =document.getElementById("houseMaps").value
	    var fileType = file.substring(file.lastIndexOf(".")+1);
	    if(fileType!="png"&&fileType!="jpg"&&fileType!="txt"&&fileType!="docx"&&fileType!="pdf"&&fileType!="ppt"){
	      alert("上传文件格式错误");
	      return;
	    }
	    $.ajaxFileUpload(
	    {
            url:'/upload?appName=showcase',            //需要链接到服务器地址
            secureuri:false,
            fileElementId:'houseMaps',                        //文件选择框的id属性
            dataType: 'json',                                    //服务器返回的格式，可以是json
            success: function(data, status){
              document.getElementById('relateddocument').value=data.fileUUID;
              alert("上传成功！");
              // alert(data.fileUUID);

            },
            error: function(data, status, e){
              alert("上传失败!");
            }
          }
        );
	};
    function downloadFile(index){
    	console.log(index);
    	var tmpRow = datagrid.datagrid('getData').rows[index];
        var Rd = tmpRow.relateddocument;
        console.log("Rd:"+Rd);
	    $.get("/downLoadFile?appName=showcase&fileUUID="+Rd,function(data){
	      	var dataObj=eval("("+data+")");
		    if(dataObj.success){
		    	alert("下载成功!");
		    }else{
		    	alert(dataObj.msg);
		    }
	    });
	}  ;


	//记录用户登录系统日志
	function saveSystemLog(pagetitle,page,operation,detail,userrealname,userno){
		// alert("保存日志信息");
		$.ajax({
	        type:"get",
	        dataType : "json",
	        url:"../../api/insertSystemLog",
	        data:{pagetitle:pagetitle,page:page,operation:operation,detail:detail,userrealname:userrealname,userno:userno},
	        success:function(r){
	            if (r==1){
	            	console.log("保存"+r+"条日志信息");
	          	}
	        }
      	});
	}